﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Report
{
    public partial class DelayReport : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check user permission
            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);
            if (!Page.IsPostBack)
            {
                this.FillInReportDropdown_Adjust();

                //attach client events
                this.btnExport.Attributes.Add("onClick", "RptExportToExcel('" + ReportHelper.ReportType.DelayReport.ToString() + "');return false;");
                this.btnExport.Visible = false;

                RunReport(user.EmployeeId, int.Parse(ddlAdjust.SelectedItem.Value));
            }

            //if (!ReportHelper.IsAdmin(user.EmployeeId) && !ReportHelper.AllowedDownload(user.EmployeeId))
            //{

            //    this.divError.InnerHtml = AlertMessage.NoAllowedReportData; 
            //    //this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('没有相关数据权限，请联系管理员!\nNo allow to view data please contact the admin!')</script>");
            //    this.divReport.InnerHtml = "";
            //    this.btnExport.Visible = false;
            //}
            //else
            //{
            //    //Run Report
                //RunReport(user.EmployeeId);
            //}
        }

        protected void btnRun_Click(object sender, EventArgs e)
        {
            RunReport(System.Convert.ToInt32(user.EmployeeId), int.Parse(ddlAdjust.SelectedItem.Value));
        }

        private void RunReport(long employeeID,int adjust)
        {
            string err = string.Empty;
            string reportHTML = string.Empty;
            string excelHTML = string.Empty;
            try
            {
                reportHTML = BEGDC.PreContract.BLL.ReportHelper.GetDelayReportHTML(employeeID,
                    ReportHelper.ReportType.DelayReport,
                    adjust,
                    out err,
                    out excelHTML);

                if (err != string.Empty)
                {
                    this.divReport.InnerHtml = "";
                    this.divError.InnerHtml = err;
                    this.btnExport.Visible = false;
                }
                else
                {
                    this.divReport.InnerHtml = reportHTML;
                    Session[ReportHelper.ReportType.DelayReport.ToString()] = excelHTML;
                    this.divError.InnerHtml = "";
                    this.btnExport.Visible = true;
                }
            }
            catch
            {
                this.divReport.InnerHtml = "";
                this.btnExport.Visible = false;               
                divError.InnerHtml = AlertMessage.GetReportDataError;
                //this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + AlertMessage.GetReportDataError  + "')</script>");

            }


        }

        private void FillInReportDropdown_Adjust()
        {
            BaseFunction.BindContractAdjustDropDownList(ddlAdjust, string.Empty, true);
        }
    }
}
